<template>
  <div class="login-container">
    <div class="login_box">
        <div style=" opacity: 1; margin-bottom: 30px; font-size: 40px; color: #0742b1;  font-weight: bold">欢迎注册大学拼车系统</div>
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width: 400px;">
          <el-form-item prop="userName" style="align-items: center;">
            <el-input
                size="large" v-model="data.form.userName" placeholder="请输入用户名" prefix-icon="User"/>
          </el-form-item>
          <el-form-item prop="phone" style="align-items: center;">
            <el-input
                size="large" v-model="data.form.phone" placeholder="请输入手机号" prefix-icon="User"/>
          </el-form-item>
          <el-form-item prop="password" style="align-items: center;">
            <el-input
              show-password  size="large" v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock"/>
          </el-form-item>
          <el-form-item prop="confirmPassword" style="align-items: center;">
            <el-input
                show-password  size="large" v-model="data.form.confirmPassword" placeholder="请再次输入密码" prefix-icon="Lock"/>
          </el-form-item>
          <div>
            <el-button @click="register" style="width: 100%;" size="large" type="primary" >注册</el-button>
          </div>
          <div style=" margin-top: 10px; text-align: right">
            <a style=" text-decoration: none; color: #0742b1;font-weight: bold;" href="/login">返 回 登 录</a>
          </div>
        </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive , ref } from 'vue';
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

const formRef = ref ()

const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请再次确认密码'))
  } else if (value !== data.form.password) {
    callback(new Error("两次密码不一致"))
  } else {
    callback()
  }
}

const data = reactive({
    userName: '',
    password: '',
    phone: '',
    form: {},
    rules: {
      userName: [
        {required: true, message: '请输入用户名', trigger: 'blur'}
      ],
      phone: [
        {required: true, message: '请输入手机号', trigger: 'blur'}
      ],
      password: [
        {required: true, message: '请输入密码', trigger: 'blur'}
      ],
      confirmPassword: [
        { validator: validatePass , trigger: 'blur'}
      ]
    },
})

//注册方法
const register =  () =>{
  formRef.value.validate((valid) => {
    if(valid){
      //发送数据到后端
      request.post('/register',data.form).then(res =>{
        if(res.code === '200'){
          ElMessage.success('注册成功')
        }else{
          console.log(data.form)
          ElMessage.error(res.msg)
        }
      })
    }
  })
}


</script>

<style scoped>
.login-container{
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/login.png");
  background-size: 100% 100%;
}
.login_box{
  opacity: 0.85;
  border-radius: 20px;
  width: 60%;
  top:30%;
  height: 50%;
  align-items: center;
  right:20%;
  position: absolute;
}

</style>